﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;

namespace GgCarControl
{
    public partial class DispathJLQueryForm : Form
    {
        private Database db = new OracleDatabase("Data Source=DG;User ID=dgrfid;Password=dgrfid;Unicode=True;Load Balance Timeout=1000");

        public DispathJLQueryForm()
        {
            InitializeComponent();
        }

        private void InitDefaultValue()
        {
            cbbLX.SelectedIndex = 0;
            cbbCZ.SelectedIndex = 0;

            dtpBegin.Value = DateTime.Now.Date;
            dtpEnd.Value = DateTime.Now;
        }

        private void LoadData()
        {
            StringBuilder strbSQL = new StringBuilder();
            strbSQL.Append("select TRUCKNO,YHBH,XM,XGSJ,XGNR,SXFC,DECODE(SXFC,'1','是','否') as SXFC_XS from TRUCK_COMMAND_LOG ");
            strbSQL.Append("where XGSJ>=to_date('" + dtpBegin.Value.ToString() + "','yyyy-mm-dd hh24:mi:ss') and XGSJ<=to_date('" + dtpEnd.Value.ToString() + "','yyyy-mm-dd hh24:mi:ss')");
            if (cbbLX.Text == "市提")
                strbSQL.Append(" and IOYARDID = '0'");
            else if (cbbLX.Text == "市入")
                strbSQL.Append(" and IOYARDID = '1'");

            if (!string.IsNullOrEmpty(txtTruckNo.Text))
                strbSQL.Append(" and TRUCKNO like '%" + txtTruckNo.Text + "%'");
            if (ckbFSXFC.Checked)
                strbSQL.Append(" and SXFC='0'");

            if (cbbCZ.Text == "发送指令")
                strbSQL.Append(" and SEND='1'");
            else if (cbbCZ.Text == "取消指令")
                strbSQL.Append(" and SEND='0'");

            dgvLog.AutoGenerateColumns = false;
            dgvLog.DataSource = db.ExecuteDataSet(db.GetSqlStringCommand(strbSQL.ToString())).Tables[0];
        }

        private void DispathJLQueryForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void DispathJLQueryForm_Load(object sender, EventArgs e)
        {
            InitDefaultValue();
            LoadData();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            ExportDataGridview(dgvLog, true);
        }

        private bool ExportDataGridview(DataGridView dgv, bool isShowExcle)
        {
            if (dgv.Rows.Count == 0)
                return false;
            //建立Excel对象
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Application.Workbooks.Add(true);
            excel.Visible = isShowExcle;
            //生成字段名称
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
            }
            //填充数据
            for (int i = 0; i < dgv.RowCount - 1; i++)
            {
                for (int j = 0; j < dgv.ColumnCount; j++)
                {
                    if (dgv[j, i].ValueType == typeof(string))
                    {
                        excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();
                    }
                    else
                    {
                        excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();
                    }
                }
            }
            return true;
        }
    }
}
